package com.seednia.module.core.post.command;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import com.seednia.foundation.framework.extension.command.CommandContext;
import com.seednia.foundation.framework.service.ServiceContainer;
import com.seednia.module.core.post.dto.PostCriteria;
import com.seednia.module.core.post.dto.PostDto;
import com.seednia.module.core.post.service.IPostService;

public class LoadPostCommand implements Command {

	IPostService postService;
	
	public IPostService getPostService() {
		if (postService == null)
			postService = ServiceContainer.getInstance().getByType(IPostService.class);
		return postService;
	}
	public void setPostService(IPostService postService) {
		this.postService = postService;
	}



	@Override
	public boolean execute(Context context) throws Exception {

		CommandContext cmdContext = (CommandContext)context;
		
		// This is required (Notice it is from cmdContext, as it is sent by immediate caller)
		Long postSid = (Long)cmdContext.get("sid");
		if (postSid == null)
			throw new IllegalArgumentException("Missing postSid");
		// TODO: or test with UUID
		
		PostCriteria postCriteria = new PostCriteria();
		postCriteria.setSid(postSid);
		
		PostDto result = getPostService().getPost(cmdContext.getRequestContext(), postCriteria);
		cmdContext.setRecord(result);
		
		return false;
	}

}
